package org.tloss.web.editor.model.command;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

import org.tloss.web.editor.Utils;
import org.tloss.web.editor.model.Sql;
import org.tloss.web.editor.utils.Constants;

@XmlAccessorType(XmlAccessType.PROPERTY)
public class LoadEnity extends SimpleCommand {

	private String variableName;
	private Sql sql;
	private List<ParamVariable> paramVariables;

	public List<ParamVariable> getParamVariables() {
		return paramVariables;
	}

	public void setParamVariables(List<ParamVariable> paramVariables) {
		this.paramVariables = paramVariables;
	}

	public String getVariableName() {
		return variableName;
	}

	public void setVariableName(String variableName) {
		this.variableName = variableName;
	}

	private String sqlResource;

	@XmlTransient
	public Sql getSql() {
		return sql;
	}

	public void setSql(Sql sql) {
		this.sql = sql;
	}

	@XmlElement
	public String getSqlResource() {
		return sqlResource;
	}

	public void setSqlResource(String sqlResource) {
		this.sqlResource = sqlResource;
	}

	@Override
	public String getLabel() {

		return "LoadEnity";
	}

	@Override
	public String generate(int type, Object[] data) {
		StringBuffer buffer = new StringBuffer();
		Sql sql = Utils.loadSql(sqlResource);
		if (sql != null && data != null) {
			@SuppressWarnings("unchecked")
			Map<String, Object> map = (Map<String, Object>) data[0];
			String packageName = (String) map.get("entityPackage");
			String sqlPackage = (String) map.get("sqlPackage");
			Properties properties =  (Properties) map.get("config");
			int idx1 = sqlResource.lastIndexOf("/");
			int idx2 = sqlResource.lastIndexOf(Constants.SQL_EXT);
			String sqlClass = sqlResource.substring(idx1 + 1, idx2 - 1);
			buffer.append(sqlPackage).append(".").append(sqlClass);
			buffer.append(" sql_").append(variableName);
			
			buffer.append(
					"=").append(properties.getProperty(Constants.BEANFACTORY_PRO)).append(".getBeanFactory().getBean(\"")
					.append("sql_")
					.append(sql.getTables().get(0).getTableName())
					.append("\",");
			buffer.append(sqlPackage).append(".").append(sqlClass)
					.append(".class);");
			
			buffer.append("java.util.List<").append(packageName).append(".")
					.append(sql.getTables().get(0).getTableName());
			buffer.append("> ").append(variableName).append("=");
			buffer.append("sql_").append(variableName).append(".select_")
					.append(sqlClass).append("(");
			if (paramVariables != null) {
				boolean first = true;
				for (int i = 0; i < paramVariables.size(); i++) {
					if (first) {
						buffer.append(paramVariables.get(i).getOperand()
								.generate(type, data));
						first = false;
					} else {
						buffer.append(",").append(
								paramVariables.get(i).getOperand()
										.generate(type, data));

					}
				}
			}
			buffer.append(");");
			buffer.append("model.put(\"").append(variableName).append("\",")
					.append(variableName).append(");");
		}
		return buffer.toString();
	}

}
